using System.Collections;
using System.Xml;
using System.Web;
using ChannelVN.Cached.Common;
using ChannelVN.Cached;

namespace ChannelVN.Cached.Config
{
    public class _301RedirectionRules : CollectionBase
    {
        private const string CACHE_NAME_FOR_301_REDIRECTION_SETTING_FILE = "COMMON_CONFIGURATION_301_REDIRECTION_SETTING";

        public static _301RedirectionRules GetCurrent301RedirectionRules()
        {
            object cacheData = CacheController.GetCacheDependencyInstance().Get(CACHE_NAME_FOR_301_REDIRECTION_SETTING_FILE);
            if (null != cacheData)
            {
                try
                {
                    return (_301RedirectionRules)cacheData;
                }
                catch
                {
                }
            }
            try
            {
                string configFilePath = HttpContext.Current.Server.MapPath(AppConfigs._301RedirectionRuleSettingFile); //@"D:\Running projects\VC Corporation\Dantri\Dantri.Cached\CacheSettings.config";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFilePath);

                _301RedirectionRules rules = new _301RedirectionRules();

                XmlNodeList nlstRules = xmlDoc.DocumentElement.SelectNodes("//rules/rule");

                for (int i = 0; i < nlstRules.Count; i++)
                {
                    RedirectRule rule = new RedirectRule();
                    rule.Name = nlstRules[i].Attributes["name"].Value;
                    rule.Url = nlstRules[i].SelectSingleNode("url").InnerText;
                    rule.Parameters = nlstRules[i].SelectSingleNode("params").InnerText;
                    rule.Method = nlstRules[i].SelectSingleNode("method").InnerText;

                    rules.List.Add(rule);
                }

                XmlNode nodeFileSettingCacheExpire = xmlDoc.DocumentElement.SelectSingleNode("//Configuration/RedirectRulesFile");
                long fileSettingCacheExpire = Lib.Object2Long(nodeFileSettingCacheExpire.Attributes["cacheExpire"].Value);
                if (fileSettingCacheExpire <= 0)
                {
                    fileSettingCacheExpire = 3600;// default 1h
                }

                CacheController.GetCacheDependencyInstance().Add(configFilePath, CACHE_NAME_FOR_301_REDIRECTION_SETTING_FILE, rules);
                return rules;
            }
            catch
            {
                return new _301RedirectionRules();
            }
        }

        public RedirectRule this[int index]
        {
            get
            {
                return (RedirectRule)List[index];
            }
        }

        public struct RedirectRule
        {
            public RedirectRule(string name, string url, string parameters, string method)
            {
                this.Name = name;
                this.Url = url;
                this.Parameters = parameters;
                this.Method = method;
            }
            public string Name, Url, Parameters, Method;
        }
    }
}
